package com.qubuyer.business.home.view

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.google.android.material.tabs.TabLayout
import com.qubuyer.R
import com.qubuyer.business.auth.view.LoginActivity
import com.qubuyer.business.home.adapter.HomeViewPagerAdapter
import com.qubuyer.business.mine.activity.MineWalletActivity
import com.qubuyer.constant.NetConstant
import com.qubuyer.core.ext.format
import com.qubuyer.customview.BrowserActivity
import com.qubuyer.databinding.HomeItemSaleTopBinding
import com.qubuyer.repository.entitys.HomeSaleTopEntity
import com.qubuyer.repository.entitys.UserEntity
import com.qubuyer.repository.entitys.WeekMonthYearBean
import com.qubuyer.utils.DialogUtil
import com.qubuyer.utils.ImageUtil
import com.qubuyer.utils.NavigationUtil
import com.qubuyer.utils.SessionUtil

class SaleTopItemView(lifecycleOwner: LifecycleOwner, context: Context, parent: ViewGroup, layoutId: Int) : HomeItemView<HomeSaleTopEntity, HomeItemSaleTopBinding>(lifecycleOwner, context,parent, layoutId) {

    private var user: UserEntity? = null

    override fun bindView(data: HomeSaleTopEntity) {
        dataBinding.host = this
        dataBinding.data = data
        dataBinding.user = user
        val titleList = ArrayList<String>()
        val pageList = ArrayList<View>()
        data.user?.week?.let {
            add(titleList, pageList, "周榜", it)
        }
        data.user?.month?.let {
            add(titleList, pageList, "月榜", it)
        }
        data.user?.season?.let {
            add(titleList, pageList, "季榜", it)
        }
        data.user?.year?.let {
            add(titleList, pageList, "年榜", it)
        }
        dataBinding.etlTab.tabLayout.removeAllTabs()
        for (i in titleList.indices) {
            dataBinding.etlTab.addTab(titleList[i])
        }
        val mViewPagerAdapter = HomeViewPagerAdapter(pageList)
        dataBinding.vpPage.adapter = mViewPagerAdapter
        dataBinding.vpPage.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(dataBinding.etlTab.tabLayout))
        dataBinding.etlTab.setupWithViewPager(dataBinding.vpPage)
    }

    fun bindUser(user: UserEntity) {
        this.user = user
        dataBinding.user = user
    }

    private fun add(titleList: ArrayList<String>, pageList: ArrayList<View>, title: String, bean: List<WeekMonthYearBean>) {
        if (bean.isNotEmpty()) {
            titleList.add(title)
            val page = LayoutInflater.from(mContext).inflate(R.layout.layout_fragment_home_sale_top_page, null).apply {
                findViewById<LinearLayout>(R.id.ll_container).setOnClickListener {
                    //                    Intent intent = new Intent(mContext, BrowserActivity.class);
                    //                    intent.putExtra(BrowserActivity.KEY_URL, NetConstant.BASE_URL + "/home/html/leaderboard");
                    //                    mContext.startActivity(intent);
                }
                findViewById<LinearLayout>(R.id.ll_activity_rules).setOnClickListener {
                    //                    Intent intent = new Intent(mContext, BrowserActivity.class);
                    //                    intent.putExtra(BrowserActivity.KEY_URL, NetConstant.BASE_URL + "/home/html/leaderboard");
                    //                    mContext.startActivity(intent);
                }
                if (bean.isNotEmpty()) {
                    initRankInfo(bean[0],
                            findViewById(R.id.iv_first_head_img),
                            findViewById(R.id.tv_first_user_name),
                            findViewById(R.id.tv_first_user_sale_amount),
                            findViewById(R.id.tv_first_user_award_amount)
                    )
                }
                if (bean.size >= 2) {
                    initRankInfo(bean[1],
                            findViewById(R.id.iv_second_head_img),
                            findViewById(R.id.tv_second_user_name),
                            findViewById(R.id.tv_second_user_sale_amount),
                            findViewById(R.id.tv_second_user_award_amount)
                    )
                }
                if (bean.size >= 3) {
                    initRankInfo(bean[2],
                            findViewById(R.id.iv_third_head_img),
                            findViewById(R.id.tv_third_user_name),
                            findViewById(R.id.tv_third_user_sale_amount),
                            findViewById(R.id.tv_third_user_award_amount)
                    )
                }
            }

            pageList.add(page)
        }
    }

    private fun initRankInfo(entity: WeekMonthYearBean, ivHead: ImageView, tvName: TextView, tvSaleAmount: TextView, tvAwardAmount: TextView) {
        if (null != entity.user) {
            ImageUtil.loadCircleImage(mContext, entity.user!!.head_pic, ivHead)
            tvName.text = entity.user!!.nickname
        }
        tvSaleAmount.text = "销售额:${entity.money.toDouble().format(0)}元"
        tvAwardAmount.text = "奖励:${entity.bonus.toDouble().format(0)}元"
    }

    fun viewMore(v: View) {
        if (SessionUtil.getInstance().isLogined) {
            NavigationUtil.overlay(mContext, MineWalletActivity::class.java)
        } else {
            DialogUtil.getConfirmDialog(mContext, "提示", "需要登录后，才能继续以下操作，是否现在登录？", "登录", "取消", false, { _, _ -> NavigationUtil.overlay(mContext, LoginActivity::class.java) }) { dialog, _ -> dialog.dismiss() }.show()
        }
    }

    fun showRule(v: View) {
        val intent = Intent(mContext, BrowserActivity::class.java)
        intent.putExtra(BrowserActivity.KEY_URL, NetConstant.BASE_URL + "/home/html/leaderboard")
        mContext.startActivity(intent)
    }
}